<?php

namespace App\Service;

use App\Dep\AccountDep;
use App\Dep\AuthDep;
use App\Dep\CategorysDep;
use App\Dep\GoodsCategorysDep;
use App\Dep\ModelCategorysDep;
use App\Dep\ModelDep;
use App\Dep\ProductCategorysDep;
use App\Dep\BrandCategorysDep;
use App\Dep\ProductDep;
use App\Dep\RolesDep;
use App\Dep\TeamsDep;
use App\Dep\TextToImageCategorysDep;
use App\Dep\UsersDep;
use App\Enum\CommonEnum;
use App\Enum\ContentEnum;
use App\Enum\ModelEnum;
use App\Enum\OperationsEnum;
use App\Enum\TextToImageEnum;
use App\Enum\TrainEnum;
use App\Enum\UserEnum;
use App\Enum\AccountEnum;
use App\Enum\TaskEnum;
use App\Models\ModelCategorysModel;
use App\Dep\AuthCategorysDep;
use App\Enum\SexEnum;
use App\Dep\AddressDep;
use App\Dep\MainDep;
class DictService
{
    public $dict = [];

    public function __construct()
    {

    }

    public function enumToDict($enum)
    {
        $res = [];
        foreach ($enum as $index => $item) {
            $res[] = [
                'label' => $item,
                'value' => $index,
            ];
        }
        return $res;
    }

    public function setOperationStatus()
    {
        $this->dict['operation_status'] = $this->enumToDict(OperationsEnum::$statusArr);
        return $this;
    }

    public function setAllUser()
    {
        $userDep = new UsersDep();

        $this->dict['all_user'] = $userDep->all()->map(function ($item) {
            return [
                'label' => $item['name'],
                'value' => $item['id'],
            ];
        });
        return $this;
    }

    public function setGoodsCategory()
    {

        $categoryDep = new GoodsCategorysDep();

        $arr = $categoryDep->all()->map(function ($item) {
            return [
                'label' => $item['name'],
                'value' => $item['id'],
            ];
        })->toArray();
        array_unshift($arr, ['label' => '未分类', 'value' => -1]);
        $this->dict['goods_category'] = $arr;
        return $this;

    }

    public function setAllAccount()
    {
        $userDep = new AccountDep();

        $resUser = $userDep->all();
        $this->dict['all_account'] = $resUser->map(function ($item) {
            $platformName = AccountEnum::$platformArr[$item['platform']];
            return [
                'label' => $item['username'] . $this->tokenShow($item['is_cookie_exp'], $item['is_token_exp'], $item['platform']),
                'value' => $item['id'],
                'platform' => $item['platform'],
                'platform_show' => "[{$platformName}]{$item['username']}"
            ];
        });

        $this->dict['group_account'] = $resUser->groupBy('platform')->map(function ($item, $platform) {
            return [
                "value" => $platform,
                "label" => AccountEnum::$platformArr[$platform],
                "options" => $item->map(function ($item1) {
                    return [
                        'label' => $item1['username'] . $this->tokenShow($item1['is_cookie_exp'], $item1['is_token_exp'], $item1['platform']),
                        'value' => $item1['id']
                    ];
                })
            ];
        })->values();
        return $this;
    }

    public function setNotDelAccount()
    {
        $userDep = new AccountDep();

        $resUser = $userDep->allNotDel();
        $this->dict['not_del_account'] = $resUser->map(function ($item) {
            $platformName = AccountEnum::$platformArr[$item['platform']];
            return [
                'label' => "{$item['nickname']}-{$item['username']}" . $this->tokenShow($item['is_cookie_exp'], $item['is_token_exp'], $item['platform']),
                'value' => $item['id'],
                'platform' => $item['platform'],
                'platform_show' => "[{$platformName}]{$item['username']}"
            ];
        });

        $this->dict['group_not_del_account'] = $resUser->groupBy('platform')->map(function ($item, $platform) {
            return [
                "value" => $platform,
                "label" => AccountEnum::$platformArr[$platform],
                "options" => $item->map(function ($item1) {
                    return [
                        'label' => "{$item1['nickname']}-{$item1['username']}" . $this->tokenShow($item1['is_cookie_exp'], $item1['is_token_exp'], $item1['platform']),
                        'value' => $item1['id']
                    ];
                })
            ];
        })->values();
        return $this;
    }

    public function setOnlyAllAccount()
    {

        $userDep = new AccountDep();

        $resUser = $userDep->all();
        $this->dict['only_all_account'] = $resUser->map(function ($item) {
            $platformName = AccountEnum::$platformArr[$item['platform']];
            return [
                'label' => "{$item['nickname']}-{$item['username']}" . ($item['is_del'] == CommonEnum::YES ? '(已删除)' : ''),
                'value' => $item['id'],
                'platform' => $item['platform'],
                'platform_show' => "[{$platformName}]{$item['username']}"
            ];
        });

        $this->dict['group_only_account'] = $resUser->groupBy('platform')->map(function ($item, $platform) {
            return [
                "value" => $platform,
                "label" => AccountEnum::$platformArr[$platform],
                "options" => $item->map(function ($item1) {
                    return [
                        'label' => "{$item1['nickname']}-{$item1['username']}" . ($item1['is_del'] == CommonEnum::YES ? '(已删除)' : ''),
                        'value' => $item1['id']
                    ];
                })
            ];
        })->values();
        return $this;
    }

    private function tokenShow($isCookieExp, $istokenExp, $platform)
    {
        if ($platform == ContentEnum::PLATFORM_JINGDONG) {
            if ($isCookieExp == CommonEnum::YES) {
                return "(授权未开)";
            } else {
                return "(授权已开)";
            }
        }

        if ($platform == ContentEnum::PLATFORM_TAOBAO) {
            if ($istokenExp == CommonEnum::YES) {
                return "(授权未开)";
            } else {
                return "(授权已开)";
            }
        }
    }

    public function setAllType()
    {

        $this->dict['all_type'] = $this->enumToDict(ContentEnum::$typeArr);
        return $this;

    }

    public function setVideoChannel()
    {
        $this->dict['video_channel'] = $this->enumToDict(ContentEnum::$channelArr);
        return $this;
    }

    public function setAllChannel()
    {
        $this->dict['all_channel'] = $this->enumToDict(ContentEnum::$allChannelArr);
        return $this;
    }

    public function setPictureChannel()
    {
        $this->dict['picture_channel'] = $this->enumToDict(ContentEnum::$pictureChannelArr);
        return $this;
    }

    public function setGroupChannel()
    {
        $this->dict['group_channel'] = ContentEnum::$groupChannelArr;
        return $this;
    }

    public function setPictureGroupChannel()
    {
        $this->dict['picture_group_channel'] = ContentEnum::$pictureGroupChannelArr;
        return $this;
    }

    public function setProductCategory()
    {
        $dep = new ProductCategorysDep();
        $resArr = $dep->all();

        $this->dict['product_category'] = $resArr->map(function ($item) {
            return [
                'label' => $item->name,
                'value' => $item->id,
            ];
        });

        return $this;
    }


    public function setAllCategory()
    {
        $categoryDep = new CategorysDep();

        $arr = $categoryDep->all()->map(function ($item) {
            return [
                'label' => $item['name'],
                'value' => $item['id'],
            ];
        })->toArray();
        array_unshift($arr, ['label' => '未分类', 'value' => -1]);
        $this->dict['all_category'] = $arr;
        return $this;
    }

    public function setRole()
    {
        $rolesDep = new RolesDep();

        $this->dict['all_role'] = $rolesDep->all()->map(function ($item) {
            return [
                'label' => $item['name'],
                'value' => $item['id'],
            ];
        });
        return $this;
    }

    public function setTeam()
    {
        $teamsDep = new TeamsDep();
        $this->dict['all_team'] = $teamsDep->all()->map(function ($item) {
            return [
                'label' => $item['name'],
                'value' => $item['id'],
            ];
        });
        return $this;
    }

    public function setUserType()
    {
        $this->dict['all_user_type'] = $this->enumToDict(UserEnum::$userTypeArr);
        return $this;
    }

    public function setUserStatus()
    {
        $this->dict['all_user_status'] = $this->enumToDict(UserEnum::$userStatusArr);
        return $this;
    }

    public function setOnlineStatus()
    {
        $this->dict['all_online_status'] = $this->enumToDict(UserEnum::$onlineStatusArr);
        return $this;
    }

    public function setContentStatus()
    {
        $this->dict['content_status'] = $this->enumToDict(ContentEnum::$statusArr);
        return $this;
    }

    public function setTaskStatus()
    {
        $this->dict['task_status'] = $this->enumToDict(TaskEnum::$statusArr);
        return $this;
    }

    public function setProduct()
    {

        $dep = new ProductDep();
        $categoryDep = new ProductCategorysDep();

        $resGroup = $categoryDep->all()->keyBy('id');

        $resProduct = $dep->all();
        $res = $resProduct->groupBy('category_id')->map(function ($item, $category) use ($resGroup) {
            return [
                'label' => $resGroup->get($category) ? $resGroup->get($category)->name : '未分类',
                'value' => $category,
                'options' => $item->map(function ($product) {
                    return [
                        'label' => $product->name,
                        'value' => $product->id
                    ];
                })
            ];
        })->values();

        $this->dict['group_product'] = $res;
        return $this;
    }

    public function setProductTree(){

        $cateDep = new ProductCategorysDep();
        $resAll = $cateDep->all();

        $resCategory = $resAll->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'parent_id' => $item->parent_id,
            ];
        });

        $productDep = new ProductDep();
        $resProduct = $productDep->allOk()->map(function($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'is_last' => true,
                'parent_id' => $item->category_id,
            ];
        });

        $this->dict['product_tree'] = listToTree($resCategory->merge($resProduct)->toArray(),-1);
        return $this;

    }

    public function setProductCategoryTree()
    {

        $dep = new ProductCategorysDep();

        $resCategory = $dep->all()->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'parent_id' => $item->parent_id,
            ];
        });
        $this->dict['product_category_tree'] = listToTree($resCategory->toArray(), -1);
        return $this;
    }
    public function setAuthCategoryTree()
    {

        $dep = new AuthCategorysDep();

        $resCategory = $dep->all()->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'parent_id' => $item->parent_id,
            ];
        });
        $this->dict['auth_category_tree'] = listToTree($resCategory->toArray(), -1);
        return $this;
    }
    public function setAuthSex()
    {
        $sexenum = new SexEnum();
        $resGender = collect($sexenum::$genderArr)->map(function ($label, $value) {
            return [
                'label' => $label,
                'value' => $value,
            ];
        });

        $this->dict['gender'] = $resGender->values()->all(); // Use values() to reset the array keys
        return $this;
    }
    public function setAuthAdressTree()
    {
        $dep = new AddressDep();

        $resCategory = $dep->all()->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'parent_id' => $item->parent_id,
            ];
        });
        $this->dict['auth_address_tree'] = listToTree($resCategory->toArray(), -1);
        return $this;
    }

    public function setAuthCategory() {
        $dep = new AuthCategorysDep();

        // 获取所有 AuthCategorysDep 的记录
        $resList = $dep->all();

        // 筛选出 parent_id == -1 的记录，并将其转换为所需的结构
        $this->dict['first_category'] = $resList->filter(function ($item) {
            return $item->parent_id == -1;
        })->map(function ($item) {
            return [
                'label' => $item->name,
                'value' => $item->id
            ];
        })->toArray();

        return $this;
    }



    public function setBrandCategory()
    {

        $dep = new BrandCategorysDep();

        $resList = $dep->all();

        $this->dict['brand_category'] = $resList->map(function ($item) {
            return [
                'label' => $item->brand,
                'value' => $item->id
            ];
        })->toArray();


//        array_unshift($this->dict['brand_category'],['label'=>'未分类','value'=> -1]);

        return $this;
    }

    public function setAuth()
    {
        $dep = new AuthDep();
        $resAll = $dep->allNotDel();

        $this->dict['auth'] = $resAll->map(function ($item) {
            return [
                'label' => $item->name,
                'value' => $item->id
            ];
        });
        return $this;
    }

    public function setAuthTree(){

        $cateDep = new AuthCategorysDep();
        $resAll = $cateDep->all();

        $resCategory = $resAll->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'parent_id' => $item->parent_id,
            ];
        });

        $authDep = new AuthDep();
        $resLora = $authDep->allNotDel()->map(function($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'is_last' => true,
                'parent_id' => $item->category_id,
            ];
        });
        $this->dict['auth_tree'] = listToTree($resCategory->merge($resLora)->toArray(),-1);
        return $this;

    }

    public function setModelCategoryTree(){

        $dep = new ModelCategorysDep();

        $resCategory = $dep->all()->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'parent_id' => $item->parent_id,
            ];
        });

        $this->dict['model_category_tree'] = listToTree($resCategory->toArray(), -1);
        return $this;
    }

    public function setModelSize(){
        $this->dict['train_size'] = $this->enumToDict(TrainEnum::$sizeArr);
        return $this;
    }

    public function setModelMold(){
        $this->dict['model_mold'] = $this->enumToDict(TrainEnum::$moldArr);
        return $this;
    }

    public function setModelStatus(){
        $this->dict['model_status'] = $this->enumToDict(TrainEnum::$statusArr);
        return $this;
    }

    public function setLora(){

        $dep = new ModelDep();
        $resAll = $dep->allOk();

        $this->dict['lora'] = $resAll->map(function($item){
           return [
             'label' => $item->name,
             'value' => $item->id,
           ];
        });
        return $this;
    }

    public function setTextToImageCategoryTree(){

        $dep = new TextToImageCategorysDep();

        $resCategory = $dep->all()->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'parent_id' => $item->parent_id,
            ];
        });

        $this->dict['text_to_image_category_tree'] = listToTree($resCategory->toArray(), -1);
        return $this;
    }

    public function setLoraTree(){

        $cateDep = new ModelCategorysDep();
        $resAll = $cateDep->all();

        $resCategory = $resAll->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'parent_id' => $item->parent_id,
            ];
        });

        $loraDep = new ModelDep();
        $resLora = $loraDep->allOk()->map(function($item) {
            return [
                'id' => $item->id,
                'label' => $item->name,
                'value' => $item->id,
                'parent_id' => $item->second_category,
                'is_last' => true,
            ];
        });

        $this->dict['lora_tree'] = listToTree($resCategory->merge($resLora)->toArray(),-1);
        return $this;
    }

    public function setTextSize(){

        $this->dict['text_size'] = $this->enumToDict(TrainEnum::$textSizeArr);
        return $this;

    }

    public function setTextStatus(){

        $this->dict['text_status'] = $this->enumToDict(TextToImageEnum::$statusArr);
        return $this;

    }

    public function setVersionTitleContent($version)
    {
        $items = MainDep::where('version', $version)->get(); // 获取所有记录

        $this->dict['abc'] = $items->map(function ($item) {
            return [
                'version' => $item->version,
                'title' => $item->title,
                'content' => $item->content
            ];
        })->toArray();

        return $this;
    }


    public function getDict()
    {
        return $this->dict;
    }
}


